package com.lambkit.module.cms.core.search.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.lambkit.module.cms.core.search.FileSearcher;
import com.lambkit.module.cms.rpc.model.CmsCategory;
import com.lambkit.module.cms.rpc.model.CmsFile;
import com.lambkit.module.upms.rpc.model.UpmsUser;

public class LuceneFileSearcher implements FileSearcher {

	@Override
	public Page<FileSearchRecord> search(CmsFile fileInfo,String keyWord,Date endTime,int pageNo,Integer flag) {
		List<Long> classIds=new ArrayList<Long>();
		if(fileInfo.getFileCategoryId()!=1L)
		{    
			List<CmsCategory> childIds = CmsCategory.service().findAllChildById(fileInfo.getFileCategoryId(), fileInfo.getFileUserId());
			if (childIds != null) {
				for (int i = 0; i < childIds.size(); i++) {
					classIds.add(childIds.get(i).getCategoryId());
				}
			}
			classIds.add(fileInfo.getFileCategoryId());
		}else
		{
			classIds=null;
		}
		List<FileSearchRecord> list=LuceneSearchUtil.indexFileSearch(fileInfo,keyWord,endTime,pageNo,10,classIds, flag);
		Page<FileSearchRecord> page=new Page<FileSearchRecord>();
		page.setPageNumber(pageNo);
		page.setList(list);
		page.setPageSize(10);
		page.setTotalPage(LuceneSearchUtil.totalPage());
		return page;
	}

	@Override
	public void addFileIndex(CmsFile fileInfo,String fileUserName,String fileText) {
		LuceneIndexUtil.addFileIndex(fileInfo,fileUserName,fileText);
	}

	@Override
	public void deleteFileIndex(CmsFile fileInfo) {
		LuceneIndexUtil.deteleFileIndex(fileInfo);
	}

	@Override
	public List<String> getKeyWord(CmsFile fileInfo, Long size) {
		// TODO Auto-generated method stub
		return LuceneSearchUtil.extractKeyword(fileInfo.getFileId(),size);
	}

	@Override
	public String getSummary(CmsFile fileInfo, Long size) {
		// TODO Auto-generated method stub
		return LuceneSearchUtil.extractSummary(fileInfo.getFileId(),size);
	}

	@Override
	public List<Long> getRelation(Long fileId) {
		// TODO Auto-generated method stub
		return LuceneSearchUtil.extractRelation(fileId);
	}

	@Override
	public List<SerResult> getParagraph(String keyWord,Long size) {
		// TODO Auto-generated method stub
		return LuceneSearchUtil.extractParagrap(keyWord,size);
	}

	@Override
	public void deleteAllIndex() {
		// TODO Auto-generated method stub
		List<CmsFile> lists=CmsFile.service().getFilesByState(5);
		for(CmsFile list:lists)
		{
			LuceneIndexUtil.deteleFileIndex(list);
		}
	}

	@Override
	public void addAllIndex() {
		// TODO Auto-generated method stub
		List<CmsFile> lists=CmsFile.service().getFilesByState(5);
		for(CmsFile list:lists)
		{
			String fileText = LuceneSearchUtil.judge(list.getFileId());
			UpmsUser user = UpmsUser.service().findById(list.getFileUserId());
			String userName = user!=null ? user.getUsername() : null;
			LuceneIndexUtil.addFileIndex(list, userName,fileText);
		}
	}
}
